
# =============================================================
# File: geography_barplots.R
# Purpose: Plots study counts
# Date: 19 July 2019
# Data: ./Data_maps.xlsx
#
# Techincal disclaimer:
# All analyses in R version 3.5.0 (2018-04-23) using RStudio Version 1.1.453 on Windows 10, 64 bit
#	Machine: Intel(R) Core(TM) i7-4650U CPU @ 1.70Ghz 2.30Ghz with 8GB RAM
# =============================================================

# Clear memory
rm(list=ls())

# Load library
library(readxl)
library(foreign)
library(plyr)

# Load data
data <- read_excel("./Data_maps.xlsx")


# =============================================================
# Plot begins here: Country bar plot by non-electrified population
# =============================================================


# Set up plotting margins
par(mar = c(6,4,1,4)) 
# Color settings: colorblind-friendly palette
# http://www.cookbook-r.com/Graphs/Colors_(ggplot2)/#a-colorblind-friendly-palette
cols <- c("#999999", "#E69F00", "#56B4E9", "#009E73", "#F0E442", "#0072B2", "#D55E00", "#CC79A7")

# Get x coordinates for barplot
barx <- barplot(data$count,axes=FALSE,xlab="",ylab="")

barplot(data$count, ylab="",names.arg=data$country,las=2,col=cols[1],axes=FALSE,ylim=c(0,8))
#axis(side=2,line=-0.5,labels=FALSE)

#unelec <- (1-data$gtf_elec/100)*data$pop2017wdi
z <- data$count/(((100-data$gtf_elec)/100)*(data$pop2017wdi/1000000))
  
# Get x coordinates for barplot
points(x=barx,y=z*(8/1),col=cols[2],pch=17,cex=1.3)
axis(side=4,at=c(0,2,4,6,8),labels=c(0,0.25,0.5,0.75,1),line=-0.5)
axis(side=2,at=c(0,1,2,3,4,5,6,7,8),line=-0.5)


# Add axis names
mtext(side=2,at=c(8/2),line=2,"Count of studies")
mtext(side=4,at=c(8/2),line=2,"Studies/Unelectrified population (million)")

# Save plot as object
out <- recordPlot()
dev.off()

# Create PDF output
png("Boxplot_byunelec.png",height=480,width=480, unit="px")
out
dev.off()

# =============================================================



# =============================================================
# Plot begins here: Regions boxplot by unelectrified population
# =============================================================

data$region_unelec.pop <- (1-(data$region_gtf_elec/100))*data$region_pop2017wdi
region.count <- aggregate(data$count,by=list(data$region),FUN=sum)
region.pop <- aggregate(data$region_pop2017wdi,by=list(data$region),FUN=mean)
region.unelec <- aggregate(data$region_unelec.pop,by=list(data$region),FUN=mean)

# Set up plotting margins
par(mar = c(7,4,2,4)) 
# Color settings: colorblind-friendly palette
# http://www.cookbook-r.com/Graphs/Colors_(ggplot2)/#a-colorblind-friendly-palette
cols <- c("#999999", "#E69F00", "#56B4E9", "#009E73", "#F0E442", "#0072B2", "#D55E00", "#CC79A7")


# Get x coordinates for barplot
barx <- barplot(region.count$x,axes=FALSE,xlab="",ylab="")

barplot(region.count$x, ylab="",names.arg=region.count$Group.1,las=2,col=cols[1],axes=FALSE,ylim=c(0,15))
axis(side=2,at=c(0,3,6,9,12,15),line=-0.5)

z <- region.count$x/(region.unelec$x/1000000)

# Get x coordinates for barplot
points(x=barx,y=z*(15/0.2),col=cols[2],pch=17,cex=1.3)
axis(side=4,at=c(0,7.5/2,7.5,7.5*3/2,15),labels=c(0,0.05,0.1,0.15,0.2),line=-0.5)


# Add axis names
mtext(side=2,at=c(15/2),line=2,"Count of studies")
mtext(side=4,at=c(15/2),line=2,"Studies/Unelectrified Population (million)")

# Save plot as object
out <- recordPlot()
dev.off()

# Create PDF output
png("Boxplot_region_byunelec.png",height=480,width=480, unit="px")
out
dev.off()

# =============================================================

